package com.amarsoft.are.log.impl.simple;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

public class LogRecord
  implements Serializable
{
  private static final long serialVersionUID = 1L;
  private static long globalSequenceNumber;
  private static int nextThreadId = 10;
  private static ThreadLocal<Integer> threadIds = new ThreadLocal<Integer>();
  private int level;
  private long sequenceNumber;
  private String sourceClassName;
  private String sourceMethodName;
  private String message;
  private int threadID;
  private long millis;
  private Throwable thrown;
  private String loggerName;
  private String resourceBundleName;
  private transient boolean needToInferCaller;
  private transient Object[] parameters;
  private transient ResourceBundle resourceBundle;

  public LogRecord(int paramInt, String paramString)
  {
    this.level = paramInt;
    this.message = paramString;
    synchronized (LogRecord.class)
    {
      this.sequenceNumber = (globalSequenceNumber++);
      Integer localInteger = (Integer)threadIds.get();
      if (localInteger == null)
      {
        localInteger = new Integer(nextThreadId++);
        threadIds.set(localInteger);
      }
      this.threadID = localInteger.intValue();
    }
    this.millis = System.currentTimeMillis();
    this.needToInferCaller = true;
  }

  public String getLoggerName()
  {
    return this.loggerName;
  }

  public void setLoggerName(String paramString)
  {
    this.loggerName = paramString;
  }

  public ResourceBundle getResourceBundle()
  {
    return this.resourceBundle;
  }

  public void setResourceBundle(ResourceBundle paramResourceBundle)
  {
    this.resourceBundle = paramResourceBundle;
  }

  public String getResourceBundleName()
  {
    return this.resourceBundleName;
  }

  public void setResourceBundleName(String paramString)
  {
    this.resourceBundleName = paramString;
  }

  public int getLevel()
  {
    return this.level;
  }

  public void setLevel(int paramInt)
  {
    this.level = paramInt;
  }

  public long getSequenceNumber()
  {
    return this.sequenceNumber;
  }

  public void setSequenceNumber(long paramLong)
  {
    this.sequenceNumber = paramLong;
  }

  public String getSourceClassName()
  {
    if (this.needToInferCaller)
      inferCaller();
    return this.sourceClassName;
  }

  public void setSourceClassName(String paramString)
  {
    this.sourceClassName = paramString;
    this.needToInferCaller = false;
  }

  public String getSourceMethodName()
  {
    if (this.needToInferCaller)
      inferCaller();
    return this.sourceMethodName;
  }

  public void setSourceMethodName(String paramString)
  {
    this.sourceMethodName = paramString;
    this.needToInferCaller = false;
  }

  public String getMessage()
  {
    return this.message;
  }

  public void setMessage(String paramString)
  {
    this.message = paramString;
  }

  public Object[] getParameters()
  {
    return this.parameters;
  }

  public void setParameters(Object[] paramArrayOfObject)
  {
    this.parameters = paramArrayOfObject;
  }

  public int getThreadID()
  {
    return this.threadID;
  }

  public void setThreadID(int paramInt)
  {
    this.threadID = paramInt;
  }

  public long getMillis()
  {
    return this.millis;
  }

  public void setMillis(long paramLong)
  {
    this.millis = paramLong;
  }

  public Throwable getThrown()
  {
    return this.thrown;
  }

  public void setThrown(Throwable paramThrowable)
  {
    this.thrown = paramThrowable;
  }

  private void writeObject(ObjectOutputStream paramObjectOutputStream)
    throws IOException
  {
    paramObjectOutputStream.defaultWriteObject();
    paramObjectOutputStream.writeByte(1);
    paramObjectOutputStream.writeByte(0);
    if (this.parameters == null)
    {
      paramObjectOutputStream.writeInt(-1);
      return;
    }
    paramObjectOutputStream.writeInt(this.parameters.length);
    for (int i = 0; i < this.parameters.length; ++i)
      if (this.parameters[i] == null)
        paramObjectOutputStream.writeObject(null);
      else
        paramObjectOutputStream.writeObject(this.parameters[i].toString());
  }

  private void readObject(ObjectInputStream paramObjectInputStream)
    throws IOException, ClassNotFoundException
  {
    paramObjectInputStream.defaultReadObject();
    int i = paramObjectInputStream.readByte();
    int j = paramObjectInputStream.readByte();
    if (i != 1)
      throw new IOException("LogRecord: bad version: " + i + "." + j);
    int k = paramObjectInputStream.readInt();
    if (k == -1)
    {
      this.parameters = null;
    }
    else
    {
      this.parameters = new Object[k];
      for (int l = 0; l < this.parameters.length; ++l)
        this.parameters[l] = paramObjectInputStream.readObject();
    }
    if (this.resourceBundleName != null)
      try
      {
        this.resourceBundle = ResourceBundle.getBundle(this.resourceBundleName);
      }
      catch (MissingResourceException localMissingResourceException)
      {
        this.resourceBundle = null;
      }
    this.needToInferCaller = false;
  }

  private void inferCaller()
  {
    this.needToInferCaller = false;
    StackTraceElement[] arrayOfStackTraceElement = new Throwable().getStackTrace();
    StackTraceElement localStackTraceElement;
    String str;
    for (int i = 0; i < arrayOfStackTraceElement.length; ++i)
    {
      localStackTraceElement = arrayOfStackTraceElement[i];
      str = localStackTraceElement.getClassName();
      if (str.equals("com.amarsoft.are.log.impl.SimpleLog"))
        break;
    }
	int i = 0;
	while (i < arrayOfStackTraceElement.length)
    {
      localStackTraceElement = arrayOfStackTraceElement[i];
      str = localStackTraceElement.getClassName();
      if (!(str.equals("com.amarsoft.are.log.impl.SimpleLog")))
      {
        setSourceClassName(str);
        setSourceMethodName(localStackTraceElement.getMethodName());
        return;
      }
      ++i;
    }
  }
}